<?php
namespace Hawk\Controller;
use Think\Controller;
use Hawk\Model;
class LoginController extends Controller {
    public function index(){

      $this->display();
        //echo hawk_get_password('123456');
    }
    public function login(){
      // $appid = "dingoalklbvm6cbvvoabje";
      // $apps = "l4OcIe1FdYbfNGI-uefb97EZYJhEGP1rSBuFkPJkrtFrp5Z7OPCSoqISJ6gowisb";
      // $tmp_auth_code = $_GET['code'];
      // //获取ACCESS_TOKEN
      // $url = "https://oapi.dingtalk.com/sns/gettoken?appid=".$appid."&appsecret=".$apps;
      // $ch = curl_init();
      // curl_setopt($ch, CURLOPT_URL,$url);
      // curl_setopt($ch, CURLOPT_HEADER,0);
      // curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//禁止调用时就输出获取到的数据
      // curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
      // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
      // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
      // $result = curl_exec($ch);
      // curl_close($ch);
      // $access_token = json_decode($result,true)['access_token'];
      // //获取用户持久授权码
      // $code_url = "https://oapi.dingtalk.com/sns/get_persistent_code?access_token=".$access_token;
      // //$code_url = "http://crm.httianying.com/index.php/Hawk/Login/index";
      // $data = array ("tmp_auth_code"=>$tmp_auth_code);

      // //$this->assign('data',$data);
      // $html = $this->post($code_url,$data);
      // hecho($html);exit;
      
      //$data = array ("tmp_auth_code"=>$tmp_auth_code);
      //echo md5('123456')."------";
      //echo hawk_get_password('123456');exit;

    	if(IS_POST){
            $amodel = D('Hawk/Admin');
            if ($amodel->validate($amodel->_login_validate)->create(I('post.'),3)!==FALSE){
            	//hecho($amodel);die;
                if($amodel->login()===true){
                        //用户是否第一次登录
                  $model=M('Admin');
                  $res = $model->where(
                    array(
                        'username'=>array('eq',$_POST['username']),
                        ))->find();
                  redirect(U('Hawk/Index/index'));
                }
            }
            $this->error($amodel->getError());//显示错误信息
        }
        $this->display();
    	
    }
    /**
     * 模拟post进行url请求
     * @param string $url
     * @param array $post_data
     */
    private function post($url = '', $post_data = array()){
      if (empty($url) || empty($post_data)){
            return false;
      }
      $data_string = json_encode($data);
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//用post方法传送参数
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);       
      $result = curl_exec($ch);
      //var_dump(curl_error($ch));exit;
      curl_close($ch);
      return $result;
    }
    /**
     * 验证码
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
    public function Vcode(){
           $Verify = new \Think\Verify(); 
           $Verify->fontSize = 20; 
           $Verify->length = 3;
           $Verify->useNoise = false; 
           $Verify->entry();
        }
    /**
     * 注销登录
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
    public function outlogin(){
      $_SESSION=array();
      redirect(U('Hawk/Login/login'));
    }
}